namespace QMVnSoft.Invoices
{
    using QMVnSoft;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Globalization;
    using System.Reflection;
    using System.Text;

    public class AppInvoiceLib
    {
        private bool ib_AllowAddInvoice;
        private bool ib_AllowDeleteInvoice;
        private bool ib_AllowDiscountAndCommission;
        private bool ib_AllowEditInvoice;
        private bool ib_AllowExpensesControl;
        private bool ib_AllowImportTax;
        private bool ib_AllowLockInvoice;
        private bool ib_AllowMultiCurrencies;
        private bool ib_AllowMultiVAT;
        private bool ib_AllowPaymentTerm;
        private bool ib_AllowPostInvoice;
        private bool ib_AllowProductControl;
        private bool ib_AllowQuantityControl;
        private bool ib_AllowSpecialTax;
        private bool ib_AllowViewInvoice;
        private int ii_AllowCategoryControl;
        private int ii_AllowContractControl;
        private int ii_AllowDepartmentControl;
        private int ii_FreeFields = -1;
        private string is_DefaultAccountCode = "";
        private string is_DefaultActivate = "N";
        private string is_DetailFieldsOverride = "";
        private string is_DetailKeyField = "Stt0";
        private string is_DetailOrderString = "Stt_Nv";
        private string is_EnglishInvoiceName = "";
        private string is_EnglishInvoiceTitle = "";
        private string is_HistoryType = "K";
        private string is_InvoiceCode = "";
        private string is_InvoiceName = "";
        private string is_InvoiceSerial;
        private string is_InvoiceTitle = "";
        private string is_InvoiceType = "1";
        private string is_MasterFieldsOverride = "";
        private string is_MasterKeyField = "Stt";
        private string is_MasterOrderString = "Ngay_Ct, So_Ct";
        private string is_ReportFileName = "";
        private string is_TransactionType = "K";
        private AppInvoiceLibDataAccess oInvoiceMethod;

        private DataTable AccountJournalEntry(DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                DataRow row;
                if ((row2.RowState == DataRowState.Deleted) || (row2.RowState == DataRowState.Detached))
                {
                    continue;
                }
                if (row2["No_Co"].ToString() == "N")
                {
                    object[] objArray = new object[] { 1, row2["Tk"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    continue;
                }
                object[] keys = new object[] { 2, row2["Tk"] };
                row = po_Destina.Rows.Find(keys);
                if (row == null)
                {
                    row = po_Destina.NewRow();
                    row["No_Co"] = 2;
                    row["Tk"] = row2["Tk"];
                    row["Tien_Nt"] = 0;
                }
                row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt"]);
                if (row.RowState == DataRowState.Detached)
                {
                    po_Destina.Rows.Add(row);
                }
            }
            return po_Destina;
        }

        private DataTable AccountJournalEntry(bool pb_Before, object ps_DetailKey, DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                if (((row2.RowState != DataRowState.Deleted) && (row2.RowState != DataRowState.Detached)) && (!pb_Before || !row2["Stt0"].Equals(ps_DetailKey)))
                {
                    DataRow row;
                    if (row2["No_Co"].ToString() == "N")
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk"];
                        row["Ma_Dt"] = row2["Ma_Dt"];
                        row["Tien_Nt"] = row2["Tien"];
                        po_Destina.Rows.Add(row);
                    }
                    else
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk"];
                        row["Ma_Dt"] = row2["Ma_Dt"];
                        row["Tien_Nt"] = row2["Tien"];
                        po_Destina.Rows.Add(row);
                    }
                }
            }
            return po_Destina;
        }

        public virtual AppInvoiceLib Clone()
        {
            AppInvoiceLib lib = (AppInvoiceLib) base.MemberwiseClone();
            foreach (FieldInfo info in lib.GetType().GetFields())
            {
                object obj2 = info.GetValue(this);
                if (obj2 is AppInvoiceLib)
                {
                    AppInvoiceLib lib3 = ((AppInvoiceLib) obj2).Clone();
                    info.SetValue(lib, lib3);
                }
            }
            return lib;
        }

        public Color CtVAT_Status_Get(object ps_MasterKey)
        {
            switch (this.AppInvoiceMethod.CtVAT_Status_Get(this.TransactionType, ps_MasterKey))
            {
                case 1:
                    return Color.Blue;

                case 2:
                    return Color.Yellow;

                case 3:
                    return Color.Red;
            }
            return Color.Black;
        }

        public string DeletedMessage()
        {
            return "Chứng từ không xóa được.";
        }

        private string FormatNumber(decimal pi_Number, int pi_Decimal)
        {
            StringBuilder builder = new StringBuilder();
            int num = 1;
            long result = 0L;
            while (num <= (AppInstance.NumberDigits - pi_Decimal))
            {
                if (num == 1)
                {
                    builder.Insert(0, '0');
                }
                else
                {
                    builder.Insert(0, '#');
                }
                Math.DivRem((long) num, 3L, out result);
                if ((result == 0L) && ((num + 1) <= (AppInstance.NumberDigits - pi_Decimal)))
                {
                    builder.Insert(0, ',');
                }
                num++;
            }
            num = 1;
            if (pi_Decimal > 0)
            {
                builder.Append(".");
                while (num <= pi_Decimal)
                {
                    builder.Append('0');
                    num++;
                }
            }
            return pi_Number.ToString(builder.ToString());
        }

        private string FormatNumber2(decimal pi_Number, int pi_Decimal)
        {
            string str = this.FormatNumber(pi_Number, pi_Decimal);
            if (!string.IsNullOrEmpty(NumberFormatInfo.CurrentInfo.NumberGroupSeparator))
            {
                str.Replace(NumberFormatInfo.CurrentInfo.NumberGroupSeparator, "");
            }
            if (NumberFormatInfo.CurrentInfo.NumberDecimalSeparator != ".")
            {
                str.Replace(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".");
            }
            return str;
        }

        private string FormatNumber2(object pi_Number, int pi_Decimal)
        {
            string str = this.FormatNumber((decimal) pi_Number, pi_Decimal);
            if (!string.IsNullOrEmpty(NumberFormatInfo.CurrentInfo.NumberGroupSeparator))
            {
                str = str.Replace(NumberFormatInfo.CurrentInfo.NumberGroupSeparator, "");
            }
            if (NumberFormatInfo.CurrentInfo.NumberDecimalSeparator != ".")
            {
                str = str.Replace(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".");
            }
            return str;
        }

        private DataTable ItemJournalEntry(DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                DataRow row;
                object[] objArray;
                if ((row2.RowState == DataRowState.Deleted) || (row2.RowState == DataRowState.Detached))
                {
                    continue;
                }
                if (((po_Source.Columns.Contains("Tk_No") && po_Source.Columns.Contains("Tk_Co")) && (!string.IsNullOrEmpty(row2["Tk_No"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co"].ToString()))) && (((decimal) row2["Tien_Nt"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
                if (((po_Source.Columns.Contains("Tk_No2") && po_Source.Columns.Contains("Tk_Co2")) && (!string.IsNullOrEmpty(row2["Tk_No2"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co2"].ToString()))) && (((decimal) row2["Tien_Nt2"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No2"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No2"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt2"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co2"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co2"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt2"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
                if (((po_Source.Columns.Contains("Tk_No3") && po_Source.Columns.Contains("Tk_Co3")) && (!string.IsNullOrEmpty(row2["Tk_No3"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co3"].ToString()))) && (((decimal) row2["Tien_Nt3"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No3"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No3"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt3"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co3"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co3"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt3"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
                if (((po_Source.Columns.Contains("Tk_No4") && po_Source.Columns.Contains("Tk_Co4")) && (!string.IsNullOrEmpty(row2["Tk_No4"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co4"].ToString()))) && (((decimal) row2["Tien_Nt4"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No4"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No4"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt4"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co4"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co4"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt4"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
                if (((po_Source.Columns.Contains("Tk_No5") && po_Source.Columns.Contains("Tk_Co5")) && (!string.IsNullOrEmpty(row2["Tk_No5"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co5"].ToString()))) && (((decimal) row2["Tien_Nt5"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No5"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No5"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt5"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co5"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co5"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt5"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
                if (((po_Source.Columns.Contains("Tk_No6") && po_Source.Columns.Contains("Tk_Co6")) && (!string.IsNullOrEmpty(row2["Tk_No6"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co6"].ToString()))) && (((decimal) row2["Tien_Nt6"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No6"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No6"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt6"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co6"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co6"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt6"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
                if (((po_Source.Columns.Contains("Tk_No7") && po_Source.Columns.Contains("Tk_Co7")) && (!string.IsNullOrEmpty(row2["Tk_No7"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co7"].ToString()))) && (((decimal) row2["Tien_Nt7"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No7"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No7"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt7"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co7"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co7"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt7"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
                if (((po_Source.Columns.Contains("Tk_No8") && po_Source.Columns.Contains("Tk_Co8")) && (!string.IsNullOrEmpty(row2["Tk_No8"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co8"].ToString()))) && (((decimal) row2["Tien_Nt8"]) != 0M))
                {
                    objArray = new object[] { 1, row2["Tk_No8"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No8"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt8"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                    objArray = new object[] { 2, row2["Tk_Co8"] };
                    row = po_Destina.Rows.Find(objArray);
                    if (row == null)
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co8"];
                        row["Tien_Nt"] = 0;
                    }
                    row["Tien_Nt"] = ((decimal) row["Tien_Nt"]) + ((decimal) row2["Tien_Nt8"]);
                    if (row.RowState == DataRowState.Detached)
                    {
                        po_Destina.Rows.Add(row);
                    }
                }
            }
            return po_Destina;
        }

        private DataTable ItemJournalEntry(bool pb_Before, object ps_DetailKey, object ps_Ma_Dt, DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                if (((row2.RowState != DataRowState.Deleted) && (row2.RowState != DataRowState.Detached)) && (!pb_Before || !row2["Stt0"].Equals(ps_DetailKey)))
                {
                    DataRow row;
                    if (((po_Source.Columns.Contains("Tk_No") && po_Source.Columns.Contains("Tk_Co")) && (!string.IsNullOrEmpty(row2["Tk_No"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co"].ToString()))) && (((decimal) row2["Tien_Nt"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = 0;
                        po_Destina.Rows.Add(row);
                    }
                    if (((po_Source.Columns.Contains("Tk_No2") && po_Source.Columns.Contains("Tk_Co2")) && (!string.IsNullOrEmpty(row2["Tk_No2"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co2"].ToString()))) && (((decimal) row2["Tien_Nt2"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No2"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien2"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co2"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien2"];
                        po_Destina.Rows.Add(row);
                    }
                    if (((po_Source.Columns.Contains("Tk_No3") && po_Source.Columns.Contains("Tk_Co3")) && (!string.IsNullOrEmpty(row2["Tk_No3"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co3"].ToString()))) && (((decimal) row2["Tien_Nt3"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No3"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien3"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co3"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien3"];
                        po_Destina.Rows.Add(row);
                    }
                    if (((po_Source.Columns.Contains("Tk_No4") && po_Source.Columns.Contains("Tk_Co4")) && (!string.IsNullOrEmpty(row2["Tk_No4"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co4"].ToString()))) && (((decimal) row2["Tien_Nt4"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No4"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien4"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co4"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien4"];
                        po_Destina.Rows.Add(row);
                    }
                    if (((po_Source.Columns.Contains("Tk_No5") && po_Source.Columns.Contains("Tk_Co5")) && (!string.IsNullOrEmpty(row2["Tk_No5"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co5"].ToString()))) && (((decimal) row2["Tien_Nt5"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No5"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien5"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co5"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien5"];
                        po_Destina.Rows.Add(row);
                    }
                    if (((po_Source.Columns.Contains("Tk_No6") && po_Source.Columns.Contains("Tk_Co6")) && (!string.IsNullOrEmpty(row2["Tk_No6"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co6"].ToString()))) && (((decimal) row2["Tien_Nt6"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No6"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien6"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co6"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien6"];
                        po_Destina.Rows.Add(row);
                    }
                    if (((po_Source.Columns.Contains("Tk_No7") && po_Source.Columns.Contains("Tk_Co7")) && (!string.IsNullOrEmpty(row2["Tk_No7"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co7"].ToString()))) && (((decimal) row2["Tien_Nt7"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No7"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien7"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co7"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien7"];
                        po_Destina.Rows.Add(row);
                    }
                    if (((po_Source.Columns.Contains("Tk_No8") && po_Source.Columns.Contains("Tk_Co8")) && (!string.IsNullOrEmpty(row2["Tk_No8"].ToString()) && !string.IsNullOrEmpty(row2["Tk_Co8"].ToString()))) && (((decimal) row2["Tien_Nt8"]) != 0M))
                    {
                        row = po_Destina.NewRow();
                        row["No_Co"] = 1;
                        row["Tk"] = row2["Tk_No8"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien8"];
                        po_Destina.Rows.Add(row);
                        row = po_Destina.NewRow();
                        row["No_Co"] = 2;
                        row["Tk"] = row2["Tk_Co8"];
                        row["Ma_Dt"] = ps_Ma_Dt;
                        row["Tien_Nt"] = row2["Tien8"];
                        po_Destina.Rows.Add(row);
                    }
                }
            }
            return po_Destina;
        }

        public DataTable JournalEntry(DataTable po_Table)
        {
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("No_Co", typeof(int));
            table.Columns.Add(column);
            column = new DataColumn("Tk", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Tien_Nt", typeof(decimal));
            table.Columns.Add(column);
            table.PrimaryKey = new DataColumn[] { table.Columns["No_Co"], table.Columns["Tk"] };
            if (po_Table.Columns.Contains("Tk"))
            {
                table = this.AccountJournalEntry(po_Table, table);
            }
            else
            {
                table = this.ItemJournalEntry(po_Table, table);
            }
            table.DefaultView.Sort = "No_Co ASC, Tk ASC";
            return table;
        }

        public string LockedMessage(int pi_Key, bool pb_Locked)
        {
            string str = "";
            if (pi_Key == 7)
            {
                return "Chứng từ do người kh\x00e1c lập!";
            }
            if ((pi_Key == 5) && pb_Locked)
            {
                return "Chứng từ đã khóa!";
            }
            if ((pi_Key == 2) && !pb_Locked)
            {
                str = "Chứng từ đã mở!";
            }
            return str;
        }

        public string LockedTitle(bool pb_Locked)
        {
            if (pb_Locked)
            {
                return "Chứng từ không khóa được.";
            }
            return "Chứng từ không mở được.";
        }

        private DataTable LotOutQtyJournalEntry(object ps_DetailKey, DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                if (((row2.RowState != DataRowState.Deleted) && (row2.RowState != DataRowState.Detached)) && !row2["Stt0"].Equals(ps_DetailKey))
                {
                    DataRow row = po_Destina.NewRow();
                    row["Ma_Kho"] = row2["Ma_Kho"];
                    row["Ma_Vt"] = row2["Ma_Vt"];
                    row["Lo_Hang"] = row2["Lo_Hang"];
                    row["Han_Dung"] = row2["Han_Dung"];
                    row["H_Luong"] = row2["H_Luong"];
                    row["Do_Am_PT"] = row2["Do_Am_PT"];
                    row["Do_Con_PT"] = row2["Do_Con_PT"];
                    row["So_Luong"] = (row2["No_Co"].ToString() == "N") ? ((decimal) row2["So_Luong"]) : (-1M * ((decimal) row2["So_Luong"]));
                    po_Destina.Rows.Add(row);
                }
            }
            return po_Destina;
        }

        private DataTable LotQtyJournalEntry(object ps_DetailKey, object ps_Ma_KhoN, DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                if (((row2.RowState != DataRowState.Deleted) && (row2.RowState != DataRowState.Detached)) && !row2["Stt0"].Equals(ps_DetailKey))
                {
                    DataRow row = po_Destina.NewRow();
                    row["Ma_Kho"] = row2["Ma_Kho"];
                    row["Ma_Vt"] = row2["Ma_Vt"];
                    row["Lo_Hang"] = row2["Lo_Hang"];
                    row["Han_Dung"] = row2["Han_Dung"];
                    row["H_Luong"] = row2["H_Luong"];
                    row["Do_Am_PT"] = row2["Do_Am_PT"];
                    row["Do_Con_PT"] = row2["Do_Con_PT"];
                    row["So_Luong"] = -1M * ((decimal) row2["So_Luong"]);
                    po_Destina.Rows.Add(row);
                    if ((ps_Ma_KhoN != null) && !string.IsNullOrEmpty(ps_Ma_KhoN.ToString()))
                    {
                        row = po_Destina.NewRow();
                        row["Ma_Kho"] = ps_Ma_KhoN;
                        row["Ma_Vt"] = row2["Ma_Vt"];
                        row["Lo_Hang"] = row2["Lo_Hang"];
                        row["Han_Dung"] = row2["Han_Dung"];
                        row["H_Luong"] = row2["H_Luong"];
                        row["Do_Am_PT"] = row2["Do_Am_PT"];
                        row["Do_Con_PT"] = row2["Do_Con_PT"];
                        row["So_Luong"] = row2["So_Luong"];
                        po_Destina.Rows.Add(row);
                    }
                }
            }
            return po_Destina;
        }

        public bool MakeAmountWarningMessage(bool pb_Before, object pi_Posted, object ps_MasterKey, object ps_DetailKey, object pd_Ngay_Ct, object ps_Tk, object ps_Ma_Dt, DataTable po_Table, ref string ps_WarningMessage)
        {
            if (!string.IsNullOrEmpty(ps_Tk.ToString()) || !string.IsNullOrEmpty(ps_Ma_Dt.ToString()))
            {
                string str = "";
                DataTable table = new DataTable();
                DataColumn column = new DataColumn("No_Co", typeof(int));
                table.Columns.Add(column);
                column = new DataColumn("Tk", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("Ma_Dt", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("Tien_Nt", typeof(decimal));
                table.Columns.Add(column);
                if (((int) pi_Posted) != 0)
                {
                    if (po_Table.Columns.Contains("Tk"))
                    {
                        table = this.AccountJournalEntry(pb_Before, ps_DetailKey, po_Table, table);
                    }
                    else
                    {
                        table = this.ItemJournalEntry(pb_Before, ps_DetailKey, ps_Ma_Dt, po_Table, table);
                    }
                }
                str = "CREATE PROCEDURE Tk_Dt_Warning";
                str = (((((((((((((((str + "\n(" + "\n@p_Ma_Dvcs\t\tT_D004,") + "\n@p_Stt\t\t\tT_D020," + "\n@p_Ngay_Ct\t\tT_DATE,") + "\n@p_Tk\t\t\tT_D032," + "\n@p_Ten_Tk\t\tT_D128\t\tOUTPUT,") + "\n@p_Ten_Tk_E\t\tT_D128\t\tOUTPUT," + "\n@p_Ma_Dt\t\tT_D032,") + "\n@p_No_Co\t\tT_D001\t\tOUTPUT," + "\n@p_Ten_Dt\t\tT_D128\t\tOUTPUT,") + "\n@p_Ten_Dt_E\t\tT_D128\t\tOUTPUT," + "\n@p_Du_No\t\tT_TIEN\t\tOUTPUT,") + "\n@p_Du_Co\t\tT_TIEN\t\tOUTPUT," + "\n@p_Tien_Lmt\t\tT_TIEN\t\tOUTPUT") + "\n) WITH ENCRYPTION" + "\nAS") + "\nBEGIN" + "\nDECLARE @v_Year_Month\tT_D006,") + "\n@v_Ngay_Cd1\t\tT_DATE," + "\n@v_Ngay_Cd2\t\tT_DATE,") + "\n@v_Du_No\t\tT_TIEN," + "\n@v_Du_Co\t\tT_TIEN") + "\nSET @v_Year_Month = dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999') + dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99')" + "\nSET @v_Ngay_Cd1 = CONVERT(DATETIME, dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99') + '/01/' + dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999'))") + "\nSET @v_Ngay_Cd2 = @p_Ngay_Ct" + "\nIF @p_Stt = ''") + "\nSET @p_Stt = NULL" + "\nIF @p_Ma_Dt = ''") + "\nSET @p_Ma_Dt = NULL" + "\nIF @p_Tk = ''") + "\nSET @p_Tk = NULL" + "\nCREATE TABLE #SoCai (No_Co INT, Tk NVARCHAR(32), Ma_Dt NVARCHAR(32), Tien NUMERIC(26,6))";
                foreach (DataRow row in table.Rows)
                {
                    str = str + "\nINSERT INTO #SoCai (No_Co, Tk, Ma_Dt, Tien) VALUES (" + row["No_Co"].ToString() + ", '" + row["Tk"].ToString() + "', '" + row["Ma_Dt"].ToString() + "', " + this.FormatNumber2(row["Tien_Nt"], AppInstance.FCAmountDecimals) + ")";
                }
                str = str + "\nUPDATE #SoCai SET Ma_Dt = NULL FROM #SoCai a LEFT OUTER JOIN DmTk b ON a.Tk = b.Tk WHERE b.Tk_Dt <> 'C'" + "\nDELETE FROM #SoCai WHERE NOT ((@p_Tk IS NULL OR ISNULL(Tk, '') = @p_Tk) AND (@p_Ma_Dt IS NULL OR ISNULL(Ma_Dt, '') = @p_Ma_Dt))";
                if (((int) pi_Posted) == 1)
                {
                    str = (str + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 1, Ps_No FROM CdKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Tk IS NULL OR Tk = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt = @p_Ma_Dt)" + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 2, Ps_Co FROM CdKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Tk IS NULL OR Tk = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt = @p_Ma_Dt)") + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 1, Tien FROM SoCaiKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Tk IS NULL OR Tk_No = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt_No = @p_Ma_Dt) AND Posted = 1 AND Stt <> @p_Stt" + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 2, Tien FROM SoCaiKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Tk IS NULL OR Tk_Co = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt_Co = @p_Ma_Dt) AND Posted = 1 AND Stt <> @p_Stt";
                }
                else
                {
                    str = (str + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 1, Ps_No0 FROM CdKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Tk IS NULL OR Tk = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt = @p_Ma_Dt)" + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 2, Ps_Co0 FROM CdKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Tk IS NULL OR Tk = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt = @p_Ma_Dt)") + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 1, Tien FROM SoCaiKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Tk IS NULL OR Tk_No = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt_No = @p_Ma_Dt) AND Posted <> 0 AND Stt <> @p_Stt" + "\nINSERT INTO #SoCai (No_Co, Tien) SELECT No_Co = 2, Tien FROM SoCaiKT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Tk IS NULL OR Tk_Co = @p_Tk) AND (@p_Ma_Dt IS NULL OR Ma_Dt_Co = @p_Ma_Dt) AND Posted <> 0 AND Stt <> @p_Stt";
                }
                str = ((((((((((((str + "\nSELECT @v_Du_No = SUM(Tien) FROM #SoCai WHERE No_Co = 1" + "\nSELECT @v_Du_Co = SUM(Tien) FROM #SoCai WHERE No_Co = 2") + "\nSET @v_Du_No = ISNULL(@v_Du_No, 0)" + "\nSET @v_Du_Co = ISNULL(@v_Du_Co, 0)") + "\nIF @v_Du_No > @v_Du_Co" + "\nBEGIN") + "\nSELECT @p_Du_No = @v_Du_No - @v_Du_Co, @p_Du_Co = 0" + "\nEND") + "\nELSE" + "\nBEGIN") + "\nSELECT @p_Du_No = 0, @p_Du_Co = @v_Du_Co + @v_Du_No" + "\nEND") + "\nSELECT @p_Ten_Tk = RTRIM(Ten_Tk), @p_Ten_Tk_E = RTRIM(Ten_Tk_E) FROM DmTk WHERE Tk = @p_Tk" + "\nIF @p_Ma_Dt IS NOT NULL") + "\nBEGIN" + "\nSELECT @p_No_Co = No_Co, @p_Ten_Dt = RTRIM(Ten_Dt), @p_Ten_Dt_E = RTRIM(Ten_Dt_E), @p_Tien_Lmt = ISNULL(Tien_Lmt, 0) FROM DmDt WHERE Ma_Dt = @p_Ma_Dt") + "\nEND" + "\nSET @p_Ten_Tk = ISNULL(@p_Ten_Tk, '')") + "\nSET @p_Ten_Tk_E = ISNULL(@p_Ten_Tk_E, '')" + "\nSET @p_No_Co = ISNULL(@p_No_Co, '1')") + "\nSET @p_Ten_Dt = ISNULL(@p_Ten_Dt, '')" + "\nSET @p_Ten_Dt_E = ISNULL(@p_Ten_Dt_E, '')") + "\nSET @p_Du_No = ISNULL(@p_Du_No, 0)" + "\nSET @p_Du_Co = ISNULL(@p_Du_Co, 0)") + "\nSET @p_Tien_Lmt = ISNULL(@p_Tien_Lmt, 0)" + "\nEND";
                string str2 = "";
                string str3 = "";
                string str4 = "";
                string str5 = "";
                string str6 = "";
                decimal num = 0M;
                decimal num2 = 0M;
                decimal num3 = 0M;
                this.oInvoiceMethod.MakeAmountWarningMessage(AppInstance.Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Tk, ref str2, ref str3, ps_Ma_Dt, ref str4, ref str5, ref str6, str, ref num, ref num2, ref num3);
                if (pb_Before && ((num + num2) != 0M))
                {
                    if (!string.IsNullOrEmpty(ps_Tk.ToString()))
                    {
                        if (num != 0M)
                        {
                            ps_WarningMessage = "|| " + ps_Tk.ToString() + " - " + str2 + " || Dư nợ: " + this.FormatNumber(num, AppInstance.LCAmountDecimals) + " ||";
                        }
                        else
                        {
                            ps_WarningMessage = "|| " + ps_Tk.ToString() + " - " + str2 + " || Dư có: " + this.FormatNumber(num2, AppInstance.LCAmountDecimals) + " ||";
                        }
                    }
                    else if (!string.IsNullOrEmpty(ps_Ma_Dt.ToString()))
                    {
                        if (num != 0M)
                        {
                            ps_WarningMessage = "|| " + ps_Ma_Dt.ToString() + " - " + str5 + " || Dư nợ: " + this.FormatNumber(num, AppInstance.LCAmountDecimals) + " ||";
                        }
                        else
                        {
                            ps_WarningMessage = "|| " + ps_Ma_Dt.ToString() + " - " + str5 + " || Dư có: " + this.FormatNumber(num2, AppInstance.LCAmountDecimals) + " ||";
                        }
                    }
                }
                else if ((!pb_Before && ((num + num2) != 0M)) && (num3 != -1M))
                {
                    if (str4 == "1")
                    {
                        if ((num - num2) > num3)
                        {
                            ps_WarningMessage = ps_Ma_Dt.ToString() + " - " + str5 + "\nCông nợ: " + this.FormatNumber(num - num2, AppInstance.LCAmountDecimals) + "\nHạn mức: " + this.FormatNumber(num3, AppInstance.LCAmountDecimals);
                            return false;
                        }
                    }
                    else if ((num2 - num) > num3)
                    {
                        ps_WarningMessage = ps_Ma_Dt.ToString() + " - " + str5 + "\nCông nợ: " + this.FormatNumber(num2 - num, AppInstance.LCAmountDecimals) + "\nHạn mức: " + this.FormatNumber(num3, AppInstance.LCAmountDecimals);
                        return false;
                    }
                }
            }
            return true;
        }

        public DataTable MakeLotQtyTable(int pi_Max_Rows, object pi_Posted, object ps_MasterKey, object ps_DetailKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Vt, object ps_Ma_KhoN, object ps_Lo_Hang, DataTable po_Table)
        {
            if (string.IsNullOrEmpty(ps_Ma_Kho.ToString()) || string.IsNullOrEmpty(ps_Ma_Vt.ToString()))
            {
                return null;
            }
            string str = "";
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("Ma_Kho", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Ma_Vt", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Lo_Hang", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Han_Dung", typeof(DateTime));
            table.Columns.Add(column);
            column = new DataColumn("H_Luong", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Am_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Con_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("So_Luong", typeof(decimal));
            table.Columns.Add(column);
            if (((int) pi_Posted) != 0)
            {
                table = this.LotQtyJournalEntry(ps_DetailKey, ps_Ma_KhoN, po_Table, table);
            }
            str = "CREATE PROCEDURE Lo_Hang_Xuat_Kho_Get";
            str = (((((((((str + "\n(") + "\n@p_Ma_Dvcs\t\tT_D004," + "\n@p_Stt\t\t\tT_D020,") + "\n@p_Ngay_Ct\t\tT_DATE," + "\n@p_Ma_Vt\t\tT_D032,") + "\n@p_Ma_Kho\t\tT_D020," + "\n@p_Lo_Hang     T_D032,") + "\n@p_Max_Rows\t\t\tT_INT" + "\n) WITH ENCRYPTION") + "\nAS" + "\nBEGIN") + "\nSET DATEFORMAT dmy" + "\nDECLARE @v_Year_Month\tT_D006,") + "\n@v_Ngay_Cd1\t\tT_DATE," + "\n@v_Ngay_Cd2\t\tT_DATE") + "\nSET @v_Year_Month = dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999') + dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99')" + "\nSET @v_Ngay_Cd1 = CONVERT(DATETIME, dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99') + '/01/' + dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999'))") + "\nSET @v_Ngay_Cd2 = @p_Ngay_Ct" + "\nCREATE TABLE #SoCai (Lo_Hang NVARCHAR(32), Han_Dung DATETIME, H_Luong NUMERIC(10,6), Do_Am_PT NUMERIC(10,6), Do_Con_PT NUMERIC(10,6), So_Luong NUMERIC(26,6))";
            foreach (DataRow row in table.Rows)
            {
                if ((row["Ma_Kho"].Equals(ps_Ma_Kho) && row["Ma_Vt"].Equals(ps_Ma_Vt)) && !string.IsNullOrEmpty(row["Lo_Hang"].ToString()))
                {
                    row["H_Luong"] = (row["H_Luong"] == DBNull.Value) ? 0 : row["H_Luong"];
                    row["Do_Am_PT"] = (row["Do_Am_PT"] == DBNull.Value) ? 0 : row["Do_Am_PT"];
                    row["Do_Con_PT"] = (row["Do_Con_PT"] == DBNull.Value) ? 0 : row["Do_Con_PT"];
                    str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) VALUES ('" + row["Lo_Hang"].ToString() + "', " + ((row["Han_Dung"] != DBNull.Value) ? ("'" + ((DateTime) row["Han_Dung"]).ToShortDateString() + "'") : "NULL") + ", " + this.FormatNumber2(row["H_Luong"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Am_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Con_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["So_Luong"], AppInstance.QuantityDecimals) + ")";
                }
            }
            if (((int) pi_Posted) == 1)
            {
                str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho FROM CdVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> '' AND Posted = 1 AND Stt <> @p_Stt";
            }
            else
            {
                str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho0 FROM CdVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> '' AND Posted <> 0 AND Stt <> @p_Stt";
            }
            string str2 = " Lo_Hang";
            if (AppInstance.DeadLineCtrl)
            {
                str2 = str2 + ", Han_Dung";
            }
            if (AppInstance.QualityCtrl)
            {
                str2 = str2 + ", ISNULL(H_Luong, 0) AS H_Luong";
            }
            if (AppInstance.DampCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Am_PT, 0) AS Do_Am_PT";
            }
            if (AppInstance.AlcoholCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Con_PT, 0) AS Do_Con_PT";
            }
            str2 = str2 + ", So_Luong";
            str = (str + "\nSELECT Lo_Hang, MAX(Han_Dung) AS Han_Dung, MAX(H_Luong) AS H_Luong, MAX(Do_Am_PT) AS Do_Am_PT, MAX(Do_Con_PT) AS Do_Con_PT, SUM(So_Luong) AS So_Luong INTO #SoCai0 FROM #SoCai WHERE NOT (So_Luong IS NULL OR So_Luong = 0) GROUP BY Lo_Hang") + "\nIF EXISTS(SELECT * FROM #SoCai0 WHERE LEFT(Lo_Hang, LEN(@p_Lo_Hang)) = @p_Lo_Hang) AND @p_Max_Rows > 0" + "\nBEGIN";
            str = ((((str + "\nSELECT TOP " + this.FormatNumber2((decimal) pi_Max_Rows, 0) + str2 + " FROM #SoCai0 WHERE NOT (So_Luong IS NULL OR So_Luong = 0) AND LEFT(Lo_Hang, LEN(@p_Lo_Hang)) = @p_Lo_Hang ORDER BY Han_Dung") + "\nEND") + "\nELSE" + "\nBEGIN") + "\nSELECT " + str2 + " FROM #SoCai0 WHERE NOT (So_Luong IS NULL OR So_Luong = 0) ORDER BY Han_Dung") + "\nEND" + "\nEND";
            return this.AppInvoiceMethod.MakeLotQuantity(AppInstance.Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Vt, ps_Lo_Hang, str, pi_Max_Rows);
        }

        public bool MakeLotQtyWarningMessage(object pi_Posted, object ps_MasterKey, object ps_DetailKey, object pd_Ngay_Ct, object ps_Nh_Ct, object ps_Ma_Kho, object ps_Ma_Vt, object ps_Ma_KhoN, object ps_Lo_Hang, DataTable po_Table, ref string ps_WarningMessage)
        {
            if ((!string.IsNullOrEmpty(ps_Ma_Kho.ToString()) && !string.IsNullOrEmpty(ps_Ma_Vt.ToString())) && !string.IsNullOrEmpty(ps_Lo_Hang.ToString()))
            {
                string str = "";
                DataTable table = new DataTable();
                DataColumn column = new DataColumn("Ma_Kho", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("Ma_Vt", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("Lo_Hang", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("So_Luong", typeof(decimal));
                table.Columns.Add(column);
                if (((int) pi_Posted) != 0)
                {
                    table = this.QtyJournalEntry(false, ps_Nh_Ct, ps_DetailKey, ps_Ma_KhoN, po_Table, table);
                }
                str = "CREATE PROCEDURE VT_KhoKT_Lo_Hang_Warning";
                str = ((((((((((((((str + "\n(" + "\n@p_Ma_Dvcs\t\tT_D004,") + "\n@p_Stt\t\t\tT_D020," + "\n@p_Ngay_Ct\t\tT_DATE,") + "\n@p_Ma_Vt\t\tT_D032," + "\n@p_Ten_Vt\t\tT_D128\t\tOUTPUT,") + "\n@p_Ten_Vt_E\t\tT_D128\t\tOUTPUT," + "\n@p_Dvt\t\t\tT_D020\t\tOUTPUT,") + "\n@p_Dvt_E\t\tT_D020\t\tOUTPUT," + "\n@p_Ma_Kho\t\tT_D020,") + "\n@p_Ten_Kho\t\tT_D128\t\tOUTPUT," + "\n@p_Ten_Kho_E\tT_D128\t\tOUTPUT,") + "\n@p_Lo_Hang T_D032," + "\n@p_Ton_Kho\t\tT_LUONG\t\tOUTPUT") + "\n) WITH ENCRYPTION" + "\nAS") + "\nBEGIN" + "\nDECLARE @v_Year_Month\tT_D006,") + "\n@v_Ngay_Cd1\t\tT_DATE," + "\n@v_Ngay_Cd2\t\tT_DATE") + "\nSET @v_Year_Month = dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999') + dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99')" + "\nSET @v_Ngay_Cd1 = CONVERT(DATETIME, dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99') + '/01/' + dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999'))") + "\nSET @v_Ngay_Cd2 = @p_Ngay_Ct" + "\nIF @p_Stt = ''") + "\nSET @p_Stt = NULL" + "\nIF @p_Ma_Kho = ''") + "\nSET @p_Ma_Kho = NULL" + "\nIF @p_Ma_Kho = ''") + "\nSET @p_Ma_Kho = NULL" + "\nCREATE TABLE #SoCai (Ma_Kho NVARCHAR(32), Ma_Vt NVARCHAR(32), So_Luong NUMERIC(26,6))";
                foreach (DataRow row in table.Rows)
                {
                    if ((row["Ma_Kho"].Equals(ps_Ma_Kho) && row["Ma_Vt"].Equals(ps_Ma_Vt)) && row["Lo_Hang"].Equals(ps_Lo_Hang))
                    {
                        str = str + "\nINSERT INTO #SoCai (So_Luong) VALUES (" + this.FormatNumber2(row["So_Luong"], AppInstance.QuantityDecimals) + ")";
                    }
                }
                if (((int) pi_Posted) == 1)
                {
                    str = str + "\nINSERT INTO #SoCai (So_Luong) SELECT Ton_Kho FROM CdVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt) AND Lo_Hang = @p_Lo_Hang" + "\nINSERT INTO #SoCai (So_Luong) SELECT So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt) AND Lo_Hang = @p_Lo_Hang AND Posted = 1 AND Stt <> @p_Stt";
                }
                else
                {
                    str = str + "\nINSERT INTO #SoCai (So_Luong) SELECT Ton_Kho0 FROM CdVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt) AND Lo_Hang = @p_Lo_Hang" + "\nINSERT INTO #SoCai (So_Luong) SELECT So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt) AND Lo_Hang = @p_Lo_Hang AND Posted <> 0 AND Stt <> @p_Stt";
                }
                str = (((((str + "\nSELECT @p_Ton_Kho = SUM(So_Luong) FROM #SoCai" + "\nSELECT @p_Ten_Vt = RTRIM(Ten_Vt), @p_Ten_Vt_E = RTRIM(Ten_Vt_E), @p_Dvt = RTRIM(Dvt), @p_Dvt_E = RTRIM(Dvt_E) FROM DmVt WHERE Ma_Vt = @p_Ma_Vt") + "\nSELECT @p_Ten_Kho = RTRIM(Ten_Kho), @p_Ten_Kho_E = RTRIM(Ten_Kho_E) FROM DmKho WHERE Ma_Kho = @p_Ma_Kho" + "\nSET @p_Ton_Kho = ISNULL(@p_Ton_Kho, 0)") + "\nSET @p_Ten_Vt = ISNULL(@p_Ten_Vt, '')" + "\nSET @p_Ten_Vt_E = ISNULL(@p_Ten_Vt_E, '')") + "\nSET @p_Dvt = ISNULL(@p_Dvt, '')" + "\nSET @p_Dvt_E = ISNULL(@p_Dvt_E, '')") + "\nSET @p_Ten_Kho = ISNULL(@p_Ten_Kho, '')" + "\nSET @p_Ten_Kho_E = ISNULL(@p_Ten_Kho_E, '')") + "\nSET @p_Ton_Kho = ISNULL(@p_Ton_Kho, 0)" + "\nEND";
                string str2 = "";
                string str3 = "";
                string str4 = "";
                string str5 = "";
                string str6 = "";
                string str7 = "";
                decimal num = 0M;
                this.oInvoiceMethod.MakeLotQtyWarningMessage(AppInstance.Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ref str6, ref str7, ps_Ma_Vt, ref str2, ref str3, ref str4, ref str5, ps_Lo_Hang, str, ref num);
                if (num < 0M)
                {
                    ps_WarningMessage = "|| " + ps_Ma_Kho.ToString() + " - " + str6 + " || " + ps_Ma_Vt.ToString() + " - " + str2.ToString() + "\nL\x00f4 hàng: " + ps_Lo_Hang.ToString() + " ||Tồn kho: " + this.FormatNumber(num, AppInstance.QuantityDecimals);
                    return false;
                }
            }
            return true;
        }

        public DataTable MakeOutLotQtyTable(int pi_Max_Rows, object pi_Posted, object ps_MasterKey, object ps_DetailKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Vt, object ps_Lo_Hang, DataTable po_Table)
        {
            if ((string.IsNullOrEmpty(ps_Ma_Kho.ToString()) || string.IsNullOrEmpty(ps_Ma_Vt.ToString())) || string.IsNullOrEmpty(ps_Lo_Hang.ToString()))
            {
                return null;
            }
            string str = "";
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("Ma_Kho", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Ma_Vt", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Lo_Hang", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Han_Dung", typeof(DateTime));
            table.Columns.Add(column);
            column = new DataColumn("H_Luong", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Am_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Con_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("So_Luong", typeof(decimal));
            table.Columns.Add(column);
            if (((int) pi_Posted) != 0)
            {
                table = this.LotOutQtyJournalEntry(ps_DetailKey, po_Table, table);
            }
            str = "CREATE PROCEDURE Lo_Hang_Xuat_Kho_Get";
            str = (((((((((str + "\n(") + "\n@p_Ma_Dvcs\t\tT_D004," + "\n@p_Stt\t\t\tT_D020,") + "\n@p_Ngay_Ct\t\tT_DATE," + "\n@p_Ma_Vt\t\tT_D032,") + "\n@p_Ma_Kho\t\tT_D020," + "\n@p_Lo_Hang     T_D032,") + "\n@p_Max_Rows\t\t\tT_INT" + "\n) WITH ENCRYPTION") + "\nAS" + "\nBEGIN") + "\nSET DATEFORMAT dmy" + "\nDECLARE @v_Year_Month\tT_D006,") + "\n@v_Ngay_Cd1\t\tT_DATE," + "\n@v_Ngay_Cd2\t\tT_DATE") + "\nSET @v_Year_Month = dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999') + dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99')" + "\nSET @v_Ngay_Cd1 = CONVERT(DATETIME, dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99') + '/01/' + dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999'))") + "\nSET @v_Ngay_Cd2 = @p_Ngay_Ct" + "\nCREATE TABLE #SoCai (Lo_Hang NVARCHAR(32), Han_Dung DATETIME, H_Luong NUMERIC(10,6), Do_Am_PT NUMERIC(10,6), Do_Con_PT NUMERIC(10,6), So_Luong NUMERIC(26,6))";
            foreach (DataRow row in table.Rows)
            {
                if ((row["Ma_Kho"].Equals(ps_Ma_Kho) && row["Ma_Vt"].Equals(ps_Ma_Vt)) && !string.IsNullOrEmpty(row["Lo_Hang"].ToString()))
                {
                    row["H_Luong"] = (row["H_Luong"] == DBNull.Value) ? 0 : row["H_Luong"];
                    row["Do_Am_PT"] = (row["Do_Am_PT"] == DBNull.Value) ? 0 : row["Do_Am_PT"];
                    row["Do_Con_PT"] = (row["Do_Con_PT"] == DBNull.Value) ? 0 : row["Do_Con_PT"];
                    str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) VALUES ('" + row["Lo_Hang"].ToString() + "', " + ((row["Han_Dung"] != DBNull.Value) ? ("'" + ((DateTime) row["Han_Dung"]).ToShortDateString() + "'") : "NULL") + ", " + this.FormatNumber2(row["H_Luong"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Am_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Con_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["So_Luong"], AppInstance.QuantityDecimals) + ")";
                }
            }
            if (((int) pi_Posted) == 1)
            {
                str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho FROM CdVTNB WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN No_Co = 'N' THEN So_Luong ELSE -So_Luong END FROM SoCaiNB WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> '' AND Posted = 1 AND Stt <> @p_Stt";
            }
            else
            {
                str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho0 FROM CdVTNB WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN No_Co = 'N' THEN So_Luong ELSE -So_Luong END FROM SoCaiNB WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> '' AND Posted <> 0 AND Stt <> @p_Stt";
            }
            string str2 = " Lo_Hang";
            if (AppInstance.DeadLineCtrl)
            {
                str2 = str2 + ", Han_Dung";
            }
            if (AppInstance.QualityCtrl)
            {
                str2 = str2 + ", ISNULL(H_Luong, 0) AS H_Luong";
            }
            if (AppInstance.DampCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Am_PT, 0) AS Do_Am_PT";
            }
            if (AppInstance.AlcoholCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Con_PT, 0) AS Do_Con_PT";
            }
            str2 = str2 + ", So_Luong";
            str = (str + "\nSELECT Lo_Hang, MAX(Han_Dung) AS Han_Dung, MAX(H_Luong) AS H_Luong, MAX(Do_Am_PT) AS Do_Am_PT, MAX(Do_Con_PT) AS Do_Con_PT, SUM(So_Luong) AS So_Luong INTO #SoCai0 FROM #SoCai WHERE NOT (So_Luong IS NULL OR So_Luong = 0) GROUP BY Lo_Hang") + "\nIF EXISTS(SELECT * FROM #SoCai0 WHERE LEFT(Lo_Hang, LEN(@p_Lo_Hang)) = @p_Lo_Hang) AND @p_Max_Rows > 0" + "\nBEGIN";
            str = ((((str + "\nSELECT TOP " + this.FormatNumber2((decimal) pi_Max_Rows, 0) + str2 + " FROM #SoCai0 WHERE NOT (So_Luong IS NULL OR So_Luong = 0) AND LEFT(Lo_Hang, LEN(@p_Lo_Hang)) = @p_Lo_Hang ORDER BY Han_Dung") + "\nEND") + "\nELSE" + "\nBEGIN") + "\nSELECT " + str2 + " FROM #SoCai0 WHERE NOT (So_Luong IS NULL OR So_Luong = 0) ORDER BY Han_Dung") + "\nEND" + "\nEND";
            return this.AppInvoiceMethod.MakeOutLotQuantity(AppInstance.Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Vt, ps_Lo_Hang, str, pi_Max_Rows);
        }

        public bool MakeQtyWarningMessage(bool pb_Before, object pi_Posted, object ps_MasterKey, object ps_DetailKey, object pd_Ngay_Ct, object ps_Nh_Ct, object ps_Ma_Kho, object ps_Ma_Vt, object ps_Ma_KhoN, DataTable po_Table, ref string ps_WarningMessage)
        {
            if (!string.IsNullOrEmpty(ps_Ma_Kho.ToString()) || !string.IsNullOrEmpty(ps_Ma_Vt.ToString()))
            {
                string str = "";
                DataTable table = new DataTable();
                DataColumn column = new DataColumn("Ma_Kho", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("Ma_Vt", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("Lo_Hang", typeof(string));
                table.Columns.Add(column);
                column = new DataColumn("So_Luong", typeof(decimal));
                table.Columns.Add(column);
                if (((int) pi_Posted) != 0)
                {
                    table = this.QtyJournalEntry(pb_Before, ps_Nh_Ct, ps_DetailKey, ps_Ma_KhoN, po_Table, table);
                }
                str = "CREATE PROCEDURE VT_KhoKT_Warning";
                str = (((((((((((((((str + "\n(") + "\n@p_Ma_Dvcs\t\tT_D004," + "\n@p_Stt\t\t\tT_D020,") + "\n@p_Ngay_Ct\t\tT_DATE," + "\n@p_Ma_Vt\t\tT_D032,") + "\n@p_Ten_Vt\t\tT_D128\t\tOUTPUT," + "\n@p_Ten_Vt_E\t\tT_D128\t\tOUTPUT,") + "\n@p_Dvt\t\t\tT_D020\t\tOUTPUT," + "\n@p_Dvt_E\t\tT_D020\t\tOUTPUT,") + "\n@p_Ma_Kho\t\tT_D020," + "\n@p_Ten_Kho\t\tT_D128\t\tOUTPUT,") + "\n@p_Ten_Kho_E\tT_D128\t\tOUTPUT," + "\n@p_Ton_Kho\t\tT_LUONG\t\tOUTPUT,") + "\n@p_SL_Ton_Min\tT_LUONG\t\tOUTPUT," + "\n@p_SL_Ton_Max\tT_LUONG\t\tOUTPUT") + "\n) WITH ENCRYPTION" + "\nAS") + "\nBEGIN" + "\nDECLARE @v_Year_Month\tT_D006,") + "\n@v_Ngay_Cd1\t\tT_DATE," + "\n@v_Ngay_Cd2\t\tT_DATE") + "\nSET @v_Year_Month = dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999') + dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99')" + "\nSET @v_Ngay_Cd1 = CONVERT(DATETIME, dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99') + '/01/' + dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999'))") + "\nSET @v_Ngay_Cd2 = @p_Ngay_Ct" + "\nIF @p_Stt = ''") + "\nSET @p_Stt = NULL" + "\nIF @p_Ma_Kho = ''") + "\nSET @p_Ma_Kho = NULL" + "\nIF @p_Ma_Kho = ''") + "\nSET @p_Ma_Kho = NULL" + "\nCREATE TABLE #SoCai (Ma_Kho NVARCHAR(32), Ma_Vt NVARCHAR(32), So_Luong NUMERIC(26,6))";
                foreach (DataRow row in table.Rows)
                {
                    str = str + "\nINSERT INTO #SoCai (Ma_Kho, Ma_Vt, So_Luong) VALUES ('" + row["Ma_Kho"].ToString() + "', '" + row["Ma_Vt"].ToString() + "', " + this.FormatNumber2(row["So_Luong"], AppInstance.QuantityDecimals) + ")";
                }
                str = str + "\nDELETE FROM #SoCai WHERE NOT ((@p_Ma_Kho IS NULL OR ISNULL(Ma_Kho, '') = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR ISNULL(Ma_Vt, '') = @p_Ma_Vt))";
                if (((int) pi_Posted) == 1)
                {
                    str = str + "\nINSERT INTO #SoCai (So_Luong) SELECT Ton_Kho FROM CdVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt)" + "\nINSERT INTO #SoCai (So_Luong) SELECT So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt) AND Posted = 1 AND Stt <> @p_Stt";
                }
                else
                {
                    str = str + "\nINSERT INTO #SoCai (So_Luong) SELECT Ton_Kho0 FROM CdVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt)" + "\nINSERT INTO #SoCai (So_Luong) SELECT So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiVT WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND (@p_Ma_Kho IS NULL OR Ma_Kho = @p_Ma_Kho) AND (@p_Ma_Vt IS NULL OR Ma_Vt = @p_Ma_Vt) AND Posted <> 0 AND Stt <> @p_Stt";
                }
                str = ((((((str + "\nSELECT @p_Ton_Kho = SUM(So_Luong) FROM #SoCai" + "\nSELECT @p_Ten_Vt = RTRIM(Ten_Vt), @p_Ten_Vt_E = RTRIM(Ten_Vt_E), @p_Dvt = RTRIM(Dvt), @p_Dvt_E = RTRIM(Dvt_E), @p_SL_Ton_Min = ISNULL(SL_Ton_Min, 0), @p_SL_Ton_Max = ISNULL(SL_Ton_Max, 0) FROM DmVt WHERE Ma_Vt = @p_Ma_Vt") + "\nSELECT @p_Ten_Kho = RTRIM(Ten_Kho), @p_Ten_Kho_E = RTRIM(Ten_Kho_E) FROM DmKho WHERE Ma_Kho = @p_Ma_Kho" + "\nSET @p_Ton_Kho = ISNULL(@p_Ton_Kho, 0)") + "\nSET @p_Ten_Vt = ISNULL(@p_Ten_Vt, '')" + "\nSET @p_Ten_Vt_E = ISNULL(@p_Ten_Vt_E, '')") + "\nSET @p_Dvt = ISNULL(@p_Dvt, '')" + "\nSET @p_Dvt_E = ISNULL(@p_Dvt_E, '')") + "\nSET @p_Ten_Kho = ISNULL(@p_Ten_Kho, '')" + "\nSET @p_Ten_Kho_E = ISNULL(@p_Ten_Kho_E, '')") + "\nSET @p_Ton_Kho = ISNULL(@p_Ton_Kho, 0)" + "\nSET @p_SL_Ton_Min = ISNULL(@p_SL_Ton_Min, 0)") + "\nSET @p_SL_Ton_Max = ISNULL(@p_SL_Ton_Max, 0)" + "\nEND";
                string str2 = "";
                string str3 = "";
                string str4 = "";
                string str5 = "";
                string str6 = "";
                string str7 = "";
                decimal num = 0M;
                decimal num2 = 0M;
                decimal num3 = 0M;
                this.oInvoiceMethod.MakeQtyWarningMessage(AppInstance.Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ref str6, ref str7, ps_Ma_Vt, ref str2, ref str3, ref str4, ref str5, str, ref num, ref num2, ref num3);
                if (pb_Before && (num != 0M))
                {
                    if (!string.IsNullOrEmpty(ps_Ma_Kho.ToString()))
                    {
                        ps_WarningMessage = "|| " + ps_Ma_Kho.ToString() + " - " + str6 + " || " + ps_Ma_Vt.ToString() + " - " + str2.ToString() + " || Tồn kho: " + this.FormatNumber(num, AppInstance.QuantityDecimals) + " ||";
                    }
                    else
                    {
                        ps_WarningMessage = "|| " + ps_Ma_Vt.ToString() + " - " + str2 + " || Tồn kho: " + this.FormatNumber(num, AppInstance.LCAmountDecimals) + " ||";
                    }
                }
                else
                {
                    if ((!pb_Before && (num2 != num3)) && ((num < num2) || (num > num3)))
                    {
                        ps_WarningMessage = "|| " + ps_Ma_Kho.ToString() + " - " + str6 + " || " + ps_Ma_Vt.ToString() + " - " + str2.ToString() + "\nTồn kho: " + this.FormatNumber(num, AppInstance.QuantityDecimals) + "\nTồn tối thiếu: " + this.FormatNumber(num2, AppInstance.QuantityDecimals) + "\nTồn tối đa: " + this.FormatNumber(num3, AppInstance.QuantityDecimals);
                        return false;
                    }
                    if (!pb_Before && (num < 0M))
                    {
                        ps_WarningMessage = "|| " + ps_Ma_Kho.ToString() + " - " + str6 + " || " + ps_Ma_Vt.ToString() + " - " + str2.ToString() + "\nTồn kho: " + this.FormatNumber(num, AppInstance.QuantityDecimals);
                        return false;
                    }
                }
            }
            return true;
        }

        public DataTable MakeStockLotQtyTable(object pi_Posted, object ps_MasterKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Vt, DataTable po_Table)
        {
            if (string.IsNullOrEmpty(ps_Ma_Kho.ToString()) || string.IsNullOrEmpty(ps_Ma_Vt.ToString()))
            {
                return null;
            }
            string str = "";
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("Ma_Kho", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Ma_Ke", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Ma_Vt", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Lo_Hang", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Han_Dung", typeof(DateTime));
            table.Columns.Add(column);
            column = new DataColumn("H_Luong", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Am_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Con_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("So_Luong", typeof(decimal));
            table.Columns.Add(column);
            if (((int) pi_Posted) != 0)
            {
                table = this.StockLotQtyJournalEntry(DBNull.Value, DBNull.Value, po_Table, table);
            }
            str = "CREATE PROCEDURE Lo_Hang_Xuat_Kho_Get";
            str = ((((((((str + "\n(") + "\n@p_Ma_Dvcs\t\tT_D004," + "\n@p_Stt\t\t\tT_D020,") + "\n@p_Ngay_Ct\t\tT_DATE," + "\n@p_Ma_Vt\t\tT_D032,") + "\n@p_Ma_Kho\t\tT_D020" + "\n) WITH ENCRYPTION") + "\nAS" + "\nBEGIN") + "\nSET DATEFORMAT dmy" + "\nDECLARE @v_Year_Month\tT_D006,") + "\n@v_Ngay_Cd1\t\tT_DATE," + "\n@v_Ngay_Cd2\t\tT_DATE") + "\nSET @v_Year_Month = dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999') + dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99')" + "\nSET @v_Ngay_Cd1 = CONVERT(DATETIME, dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99') + '/01/' + dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999'))") + "\nSET @v_Ngay_Cd2 = @p_Ngay_Ct" + "\nCREATE TABLE #SoCai (Ma_Ke NVARCHAR(32), Lo_Hang NVARCHAR(32), Han_Dung DATETIME, H_Luong NUMERIC(10,6), Do_Am_PT NUMERIC(10,6), Do_Con_PT NUMERIC(10,6), So_Luong NUMERIC(26,6))";
            foreach (DataRow row in table.Rows)
            {
                if ((row["Ma_Kho"].Equals(ps_Ma_Kho) && row["Ma_Vt"].Equals(ps_Ma_Vt)) && !string.IsNullOrEmpty(row["Lo_Hang"].ToString()))
                {
                    row["H_Luong"] = (row["H_Luong"] == DBNull.Value) ? 0 : row["H_Luong"];
                    row["Do_Am_PT"] = (row["Do_Am_PT"] == DBNull.Value) ? 0 : row["Do_Am_PT"];
                    row["Do_Con_PT"] = (row["Do_Con_PT"] == DBNull.Value) ? 0 : row["Do_Con_PT"];
                    str = str + "\nINSERT INTO #SoCai (Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) VALUES ('" + row["Ma_Ke"].ToString() + "', '" + row["Lo_Hang"].ToString() + "', " + ((row["Han_Dung"] != DBNull.Value) ? ("'" + ((DateTime) row["Han_Dung"]).ToShortDateString() + "'") : "NULL") + ", " + this.FormatNumber2(row["H_Luong"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Am_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Con_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["So_Luong"], AppInstance.QuantityDecimals) + ")";
                }
            }
            if (((int) pi_Posted) == 1)
            {
                str = str + "\nINSERT INTO #SoCai (Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho FROM CdKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> '' AND Posted = 1 AND Stt <> @p_Stt";
            }
            else
            {
                str = str + "\nINSERT INTO #SoCai (Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho0 FROM CdKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Ma_Ke, Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND ISNULL(Lo_Hang, '') <> '' AND Posted <> 0 AND Stt <> @p_Stt";
            }
            string str2 = " Lo_Hang";
            if (AppInstance.LocationCtrl)
            {
                str2 = str2 + ", Ma_Ke";
            }
            if (AppInstance.DeadLineCtrl)
            {
                str2 = str2 + ", Han_Dung";
            }
            if (AppInstance.QualityCtrl)
            {
                str2 = str2 + ", ISNULL(H_Luong, 0) AS H_Luong";
            }
            if (AppInstance.DampCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Am_PT, 0) AS Do_Am_PT";
            }
            if (AppInstance.AlcoholCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Con_PT, 0) AS Do_Con_PT";
            }
            str = ((str + "\nSELECT Ma_Ke, Lo_Hang, MAX(Han_Dung) AS Han_Dung, MAX(H_Luong) AS H_Luong, MAX(Do_Am_PT) AS Do_Am_PT, MAX(Do_Con_PT) AS Do_Con_PT, SUM(So_Luong) AS So_Luong INTO #SoCai0 FROM #SoCai WHERE NOT (So_Luong IS NULL OR So_Luong = 0) GROUP BY Ma_Ke, Lo_Hang") + "\nSELECT " + (str2 + ", So_Luong") + " FROM #SoCai0 WHERE NOT (So_Luong IS NULL OR So_Luong = 0) ORDER BY Han_Dung") + "\nEND";
            return this.AppInvoiceMethod.MakeStockLotQuantity(AppInstance.Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Vt, str);
        }

        public DataTable MakeStockLotQtyTable(int pi_Max_Rows, object pi_Posted, object ps_MasterKey, object ps_DetailKey, object pd_Ngay_Ct, object ps_Ma_Kho, object ps_Ma_Ke, object ps_Ma_Vt, object ps_Ma_KhoN, object ps_Lo_Hang, DataTable po_Table)
        {
            if ((string.IsNullOrEmpty(ps_Ma_Kho.ToString()) || string.IsNullOrEmpty(ps_Ma_Vt.ToString())) || string.IsNullOrEmpty(ps_Lo_Hang.ToString()))
            {
                return null;
            }
            string str = "";
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("Ma_Kho", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Ma_Ke", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Ma_Vt", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Lo_Hang", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Han_Dung", typeof(DateTime));
            table.Columns.Add(column);
            column = new DataColumn("H_Luong", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Am_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Do_Con_PT", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("So_Luong", typeof(decimal));
            table.Columns.Add(column);
            if (((int) pi_Posted) != 0)
            {
                table = this.StockLotQtyJournalEntry(ps_DetailKey, ps_Ma_KhoN, po_Table, table);
            }
            str = "CREATE PROCEDURE Lo_Hang_Xuat_Kho_Get";
            str = (((((((((str + "\n(" + "\n@p_Ma_Dvcs\t\tT_D004,") + "\n@p_Stt\t\t\tT_D020," + "\n@p_Ngay_Ct\t\tT_DATE,") + "\n@p_Ma_Vt\t\tT_D032," + "\n@p_Ma_Ke\t\tT_D032,") + "\n@p_Ma_Kho\t\tT_D020," + "\n@p_Lo_Hang     T_D032,") + "\n@p_Max_Rows\t\t\tT_INT" + "\n) WITH ENCRYPTION") + "\nAS" + "\nBEGIN") + "\nSET DATEFORMAT dmy" + "\nDECLARE @v_Year_Month\tT_D006,") + "\n@v_Ngay_Cd1\t\tT_DATE," + "\n@v_Ngay_Cd2\t\tT_DATE") + "\nSET @v_Year_Month = dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999') + dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99')" + "\nSET @v_Ngay_Cd1 = CONVERT(DATETIME, dbo.TRANSFORM(MONTH(@p_Ngay_Ct), 'L 99') + '/01/' + dbo.TRANSFORM(YEAR(@p_Ngay_Ct), 'L 9999'))") + "\nSET @v_Ngay_Cd2 = @p_Ngay_Ct" + "\nCREATE TABLE #SoCai (Lo_Hang NVARCHAR(32), Han_Dung DATETIME, H_Luong NUMERIC(10,6), Do_Am_PT NUMERIC(10,6), Do_Con_PT NUMERIC(10,6), So_Luong NUMERIC(26,6))";
            foreach (DataRow row in table.Rows)
            {
                if ((row["Ma_Kho"].Equals(ps_Ma_Kho) && row["Ma_Vt"].Equals(ps_Ma_Vt)) && (row["Ma_Ke"].Equals(ps_Ma_Ke) && !string.IsNullOrEmpty(row["Lo_Hang"].ToString())))
                {
                    row["H_Luong"] = (row["H_Luong"] == DBNull.Value) ? 0 : row["H_Luong"];
                    row["Do_Am_PT"] = (row["Do_Am_PT"] == DBNull.Value) ? 0 : row["Do_Am_PT"];
                    row["Do_Con_PT"] = (row["Do_Con_PT"] == DBNull.Value) ? 0 : row["Do_Con_PT"];
                    str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) VALUES ('" + row["Lo_Hang"].ToString() + "', " + ((row["Han_Dung"] != DBNull.Value) ? ("'" + ((DateTime) row["Han_Dung"]).ToShortDateString() + "'") : "NULL") + ", " + this.FormatNumber2(row["H_Luong"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Am_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["Do_Con_PT"], AppInstance.PercentDecimals) + ", " + this.FormatNumber2(row["So_Luong"], AppInstance.QuantityDecimals) + ")";
                }
            }
            if (((int) pi_Posted) == 1)
            {
                str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho FROM CdKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND Ma_Ke = @p_Ma_Ke AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND Ma_Ke = @p_Ma_Ke AND ISNULL(Lo_Hang, '') <> '' AND Posted = 1 AND Stt <> @p_Stt";
            }
            else
            {
                str = str + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, Ton_Kho0 FROM CdKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Year_Month < @v_Year_Month AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND Ma_Ke = @p_Ma_Ke AND ISNULL(Lo_Hang, '') <> ''" + "\nINSERT INTO #SoCai (Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong) SELECT Lo_Hang, Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT, So_Luong = CASE WHEN Nh_Ct = '1' THEN So_Luong ELSE -So_Luong END FROM SoCaiKho WHERE Ma_Dvcs = @p_Ma_Dvcs AND Ngay_Ct BETWEEN @v_Ngay_Cd1 AND @v_Ngay_Cd2 AND Ma_Kho = @p_Ma_Kho AND Ma_Vt = @p_Ma_Vt AND Ma_Ke = @p_Ma_Ke AND ISNULL(Lo_Hang, '') <> '' AND Posted <> 0 AND Stt <> @p_Stt";
            }
            string str2 = " Lo_Hang";
            if (AppInstance.DeadLineCtrl)
            {
                str2 = str2 + ", Han_Dung";
            }
            if (AppInstance.QualityCtrl)
            {
                str2 = str2 + ", ISNULL(H_Luong, 0) AS H_Luong";
            }
            if (AppInstance.DampCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Am_PT, 0) AS Do_Am_PT";
            }
            if (AppInstance.AlcoholCtrl)
            {
                str2 = str2 + ", ISNULL(Do_Con_PT, 0) AS Do_Con_PT";
            }
            str2 = str2 + ", So_Luong";
            str = (str + "\nSELECT Lo_Hang, MAX(Han_Dung) AS Han_Dung, MAX(H_Luong) AS H_Luong, MAX(Do_Am_PT) AS Do_Am_PT, MAX(Do_Con_PT) AS Do_Con_PT, SUM(So_Luong) AS So_Luong INTO #SoCai0 FROM #SoCai WHERE NOT (So_Luong IS NULL OR So_Luong = 0) GROUP BY Lo_Hang") + "\nIF EXISTS(SELECT * FROM #SoCai0 WHERE LEFT(Lo_Hang, LEN(@p_Lo_Hang)) = @p_Lo_Hang) AND @p_Max_Rows > 0" + "\nBEGIN";
            str = ((((str + "\nSELECT TOP " + this.FormatNumber2((decimal) pi_Max_Rows, 0) + str2 + " FROM #SoCai0 WHERE NOT (So_Luong IS NULL OR So_Luong = 0) AND LEFT(Lo_Hang, LEN(@p_Lo_Hang)) = @p_Lo_Hang ORDER BY Han_Dung") + "\nEND") + "\nELSE" + "\nBEGIN") + "\nSELECT " + str2 + " FROM #SoCai0 WHERE NOT (So_Luong IS NULL OR So_Luong = 0) ORDER BY Han_Dung") + "\nEND" + "\nEND";
            return this.AppInvoiceMethod.MakeStockLotQuantity(AppInstance.Subsidiary, ps_MasterKey, pd_Ngay_Ct, ps_Ma_Kho, ps_Ma_Ke, ps_Ma_Vt, ps_Lo_Hang, str, pi_Max_Rows);
        }

        public void MakeTransList(ref string ps_Tk_No_Co, ref string ps_TkList, ref string ps_Tien_NtList, DataTable po_SourceTable)
        {
            bool flag = true;
            bool flag2 = true;
            int num = -1;
            foreach (DataRowView view in po_SourceTable.DefaultView)
            {
                DataRow row = view.Row;
                if ((row.RowState != DataRowState.Deleted) && (row.RowState != DataRowState.Detached))
                {
                    if (flag)
                    {
                        flag = false;
                        ps_TkList = row["Tk"].ToString() + ":";
                        ps_Tien_NtList = this.FormatNumber((decimal) row["Tien_Nt"], AppInstance.FCAmountDecimals);
                    }
                    else
                    {
                        ps_TkList = ps_TkList + "\n" + row["Tk"].ToString() + ":";
                        ps_Tien_NtList = ps_Tien_NtList + "\n" + this.FormatNumber((decimal) row["Tien_Nt"], AppInstance.FCAmountDecimals);
                    }
                    if (num != ((int) row["No_Co"]))
                    {
                        num = (int) row["No_Co"];
                        if (((int) row["No_Co"]) == 1)
                        {
                            if (flag2)
                            {
                                flag2 = false;
                                ps_Tk_No_Co = "Nợ ";
                            }
                            else
                            {
                                ps_Tk_No_Co = ps_Tk_No_Co + "\nNợ";
                            }
                        }
                        else if (flag2)
                        {
                            flag2 = false;
                            ps_Tk_No_Co = "C\x00f3 ";
                        }
                        else
                        {
                            ps_Tk_No_Co = ps_Tk_No_Co + "\nC\x00f3";
                        }
                        continue;
                    }
                    if (flag2)
                    {
                        flag2 = false;
                        ps_Tk_No_Co = "";
                    }
                    else
                    {
                        ps_Tk_No_Co = ps_Tk_No_Co + "\n";
                    }
                }
            }
        }

        public string PostedMessage(int pi_Key, int pi_Posted)
        {
            string str = "";
            if (pi_Key == 9)
            {
                return "Dữ liệu đã khóa!";
            }
            if (pi_Key == 8)
            {
                return "Chứng từ đã được chọn ở phần h\x00e0nh kh\x00e1c!";
            }
            if (pi_Key == 7)
            {
                return "Chứng từ do người kh\x00e1c lập!";
            }
            if ((pi_Key == 6) && (pi_Posted == 2))
            {
                return "Chứng từ đã được duyệt!";
            }
            if ((pi_Key == 3) && (pi_Posted == 0))
            {
                str = "Chứng từ đã huỷ!";
            }
            return str;
        }

        public string PostedMessageKT(int pi_Key, int pi_Posted)
        {
            string str = "";
            if (pi_Key == 9)
            {
                return "Dữ liệu đã khóa!";
            }
            if (pi_Key == 8)
            {
                return "Chứng từ đã được chọn ở phần h\x00e0nh kh\x00e1c!";
            }
            if (pi_Key == 7)
            {
                return "Chứng từ do người kh\x00e1c lập!";
            }
            if ((pi_Key == 6) && (pi_Posted == 1))
            {
                return "Chứng từ đã cập nhật sổ c\x00e1i!";
            }
            if ((pi_Key == 3) && (pi_Posted == 2))
            {
                return "Chứng từ đã được duyệt!";
            }
            if ((pi_Key == -1) && (pi_Posted == 0))
            {
                str = "Chứng từ đã huỷ!";
            }
            return str;
        }

        public string PostedTitle(int pi_Posted)
        {
            if (pi_Posted == 1)
            {
                return "Chứng từ không cập nhật sổ c\x00e1i được.";
            }
            if (pi_Posted == 2)
            {
                return "Chứng từ không duyệt được.";
            }
            return "Chứng từ không huỷ được.";
        }

        private DataTable QtyJournalEntry(bool pb_Before, object ps_Nh_Ct, object ps_DetailKey, object ps_Ma_KhoN, DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                if (((row2.RowState != DataRowState.Deleted) && (row2.RowState != DataRowState.Detached)) && (!pb_Before || !row2["Stt0"].Equals(ps_DetailKey)))
                {
                    DataRow row = po_Destina.NewRow();
                    row["Ma_Kho"] = row2["Ma_Kho"];
                    row["Ma_Vt"] = row2["Ma_Vt"];
                    row["Lo_Hang"] = row2["Lo_Hang"];
                    row["So_Luong"] = (ps_Nh_Ct.ToString() == "1") ? ((decimal) row2["So_Luong"]) : (-1M * ((decimal) row2["So_Luong"]));
                    po_Destina.Rows.Add(row);
                    if (((ps_Nh_Ct.ToString() == "2") && (ps_Ma_KhoN != null)) && !string.IsNullOrEmpty(ps_Ma_KhoN.ToString()))
                    {
                        row = po_Destina.NewRow();
                        row["Ma_Kho"] = ps_Ma_KhoN;
                        row["Ma_Vt"] = row2["Ma_Vt"];
                        row["Lo_Hang"] = row2["Lo_Hang"];
                        row["So_Luong"] = row2["So_Luong"];
                        po_Destina.Rows.Add(row);
                    }
                }
            }
            return po_Destina;
        }

        internal void SetInvoiceParameter()
        {
            DataRow invoiceRow = this.AppInvoiceMethod.GetInvoiceRow(this.InvoiceCode);
            this.InvoiceName = invoiceRow["Ten_Ct"].ToString();
            this.EnglishInvoiceName = invoiceRow["Ten_Ct_E"].ToString();
            this.TransactionType = invoiceRow["Ma_Nvu"].ToString();
            this.HistoryType = invoiceRow["Ma_Ls"].ToString();
            this.InvoiceType = invoiceRow["Nh_Ct"].ToString();
            this.InvoiceTitle = invoiceRow["Title"].ToString();
            this.InvoiceSerial = invoiceRow["Series"].ToString();
            this.EnglishInvoiceTitle = invoiceRow["Title_E"].ToString();
            this.ReportFileName = invoiceRow["RepoFile"].ToString();
            this.DefaultActivate = (short.Parse(invoiceRow["No_Co"].ToString()) == 0) ? "N" : "C";
            this.DefaultAccountCode = invoiceRow["Tk"].ToString();
            this.AllowDepartmentControl = short.Parse(invoiceRow["BP_Ctrl"].ToString());
            this.AllowContractControl = short.Parse(invoiceRow["HD_Ctrl"].ToString());
            this.AllowCategoryControl = short.Parse(invoiceRow["KM_Ctrl"].ToString());
            this.AllowProductControl = invoiceRow["SP_Ctrl"].ToString().Equals("1");
            this.AllowMultiVAT = invoiceRow["VAT_Ctrl"].ToString().Equals("1");
            this.AllowPaymentTerm = invoiceRow["TT_Ctrl"].ToString().Equals("1");
            this.AllowImportTax = invoiceRow["NK_Ctrl"].ToString().Equals("1");
            this.AllowSpecialTax = invoiceRow["DB_Ctrl"].ToString().Equals("1");
            this.AllowDiscountAndCommission = invoiceRow["HH_Ctrl"].ToString().Equals("1");
            this.AllowExpensesControl = invoiceRow["CP_Ctrl"].ToString().Equals("1");
            this.AllowQuantityControl = invoiceRow["SL_Ctrl"].ToString().Equals("1");
            this.AllowMultiCurrencies = invoiceRow["Tte_Ctrl"].ToString().Equals("1");
            this.FreeFields = short.Parse(invoiceRow["FreeFields"].ToString());
            this.MasterFieldsOverride = "," + invoiceRow["Master_Vars"].ToString().Replace(" ", "") + ",";
            this.DetailFieldsOverride = "," + invoiceRow["Detail_Vars"].ToString().Replace(" ", "") + ",";
        }

        internal void SetUserRights()
        {
            this.AllowAddInvoice = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "V", this.InvoiceCode, 1);
            this.AllowEditInvoice = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "V", this.InvoiceCode, 2);
            this.AllowDeleteInvoice = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "V", this.InvoiceCode, 3);
            this.AllowViewInvoice = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "V", this.InvoiceCode, 4);
            this.AllowPostInvoice = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "V", this.InvoiceCode, 5);
            this.AllowLockInvoice = (AppInstance.UserType != 0) || AppDataAccess.CheckUserRights(AppInstance.UserName, "V", this.InvoiceCode, 6);
        }

        public void ShowInvoiceUsed(object ps_MasterKey, string ps_Message)
        {
            new FormInvoiceUsed(ps_Message, this.AppInvoiceMethod.RefTable_Get(ps_MasterKey)).Show();
        }

        public string StatusMessage(int pi_Key)
        {
            switch (pi_Key)
            {
                case 5:
                    return "Chứng từ đã khóa!";

                case 6:
                    return "Chứng từ đã được duyệt!";

                case 7:
                    return "Chứng từ do người kh\x00e1c lập!";

                case 8:
                    return "Chứng từ đã được chọn ở phần h\x00e0nh kh\x00e1c!";

                case 9:
                    return "Dữ liệu đã khóa!";
            }
            return "";
        }

        private DataTable StockLotQtyJournalEntry(object ps_DetailKey, object ps_Ma_KhoN, DataTable po_Source, DataTable po_Destina)
        {
            foreach (DataRow row2 in po_Source.Rows)
            {
                if (((row2.RowState != DataRowState.Deleted) && (row2.RowState != DataRowState.Detached)) && !row2["Stt0"].Equals(ps_DetailKey))
                {
                    DataRow row = po_Destina.NewRow();
                    row["Ma_Kho"] = row2["Ma_Kho"];
                    row["Ma_Ke"] = row2["Ma_Ke"];
                    row["Ma_Vt"] = row2["Ma_Vt"];
                    row["Lo_Hang"] = row2["Lo_Hang"];
                    row["Han_Dung"] = row2["Han_Dung"];
                    row["H_Luong"] = row2["H_Luong"];
                    row["Do_Am_PT"] = row2["Do_Am_PT"];
                    row["Do_Con_PT"] = row2["Do_Con_PT"];
                    row["So_Luong"] = -1M * ((decimal) row2["So_Luong"]);
                    po_Destina.Rows.Add(row);
                    if ((ps_Ma_KhoN != null) && !string.IsNullOrEmpty(ps_Ma_KhoN.ToString()))
                    {
                        row = po_Destina.NewRow();
                        row["Ma_Kho"] = ps_Ma_KhoN;
                        row["Ma_KeN"] = row2["Ma_KeN"];
                        row["Ma_Vt"] = row2["Ma_Vt"];
                        row["Lo_Hang"] = row2["Lo_Hang"];
                        row["Han_Dung"] = row2["Han_Dung"];
                        row["H_Luong"] = row2["H_Luong"];
                        row["Do_Am_PT"] = row2["Do_Am_PT"];
                        row["Do_Con_PT"] = row2["Do_Con_PT"];
                        row["So_Luong"] = row2["So_Luong"];
                        po_Destina.Rows.Add(row);
                    }
                }
            }
            return po_Destina;
        }

        [DefaultValue(false), Category("User's defined properties"), Description("Indicating whether control alcohol percent or not.")]
        public bool AlcoholPercentControl
        {
            get
            {
                return AppInstance.AlcoholCtrl;
            }
        }

        [DefaultValue(false), Category("User's defined properties"), Description("Allow this user add new invoice.")]
        public bool AllowAddInvoice
        {
            get
            {
                return this.ib_AllowAddInvoice;
            }
            set
            {
                this.ib_AllowAddInvoice = value;
            }
        }

        [DefaultValue(0), Description("Indicating whether control category or not."), Category("User's defined properties")]
        public int AllowCategoryControl
        {
            get
            {
                return this.ii_AllowCategoryControl;
            }
            set
            {
                this.ii_AllowCategoryControl = value;
            }
        }

        [Category("User's defined properties"), DefaultValue(false), Description("Allow change invoice that created by other.")]
        public bool AllowChangeInvoice
        {
            get
            {
                return AppInstance.ChangedInvoice;
            }
        }

        [Description("Indicating whether control contract or not."), DefaultValue(0), Category("User's defined properties")]
        public int AllowContractControl
        {
            get
            {
                return this.ii_AllowContractControl;
            }
            set
            {
                this.ii_AllowContractControl = value;
            }
        }

        [Category("User's defined properties"), DefaultValue(false), Description("Allow this user delete current invoice.")]
        public bool AllowDeleteInvoice
        {
            get
            {
                return this.ib_AllowDeleteInvoice;
            }
            set
            {
                this.ib_AllowDeleteInvoice = value;
            }
        }

        [Category("User's defined properties"), DefaultValue(0), Description("Indicating whether control department or not.")]
        public int AllowDepartmentControl
        {
            get
            {
                return this.ii_AllowDepartmentControl;
            }
            set
            {
                this.ii_AllowDepartmentControl = value;
            }
        }

        [DefaultValue(false), Description("Indicating whether control discount and commission or not."), Category("User's defined properties")]
        public bool AllowDiscountAndCommission
        {
            get
            {
                return this.ib_AllowDiscountAndCommission;
            }
            set
            {
                this.ib_AllowDiscountAndCommission = value;
            }
        }

        [Category("User's defined properties"), Description("Allow this user edit current invoice."), DefaultValue(false)]
        public bool AllowEditInvoice
        {
            get
            {
                return this.ib_AllowEditInvoice;
            }
            set
            {
                this.ib_AllowEditInvoice = value;
            }
        }

        [Category("User's defined properties"), DefaultValue(false), Description("Indicating whether control expenses or not.")]
        public bool AllowExpensesControl
        {
            get
            {
                return this.ib_AllowExpensesControl;
            }
            set
            {
                this.ib_AllowExpensesControl = value;
            }
        }

        [Description("Indicating whether control import tax or not."), DefaultValue(false), Category("User's defined properties")]
        public bool AllowImportTax
        {
            get
            {
                return this.ib_AllowImportTax;
            }
            set
            {
                this.ib_AllowImportTax = value;
            }
        }

        [Description("Allow this user lock current invoices."), DefaultValue(false), Category("User's defined properties")]
        public bool AllowLockInvoice
        {
            get
            {
                return this.ib_AllowLockInvoice;
            }
            set
            {
                this.ib_AllowLockInvoice = value;
            }
        }

        [DefaultValue(false), Category("User's defined properties"), Description("Indicating whether control multi currencies or not.")]
        public bool AllowMultiCurrencies
        {
            get
            {
                return this.ib_AllowMultiCurrencies;
            }
            set
            {
                this.ib_AllowMultiCurrencies = value;
            }
        }

        [Description("Indicating whether control multi VAT or not."), DefaultValue(false), Category("User's defined properties")]
        public bool AllowMultiVAT
        {
            get
            {
                return this.ib_AllowMultiVAT;
            }
            set
            {
                this.ib_AllowMultiVAT = value;
            }
        }

        [Category("User's defined properties"), DefaultValue(false), Description("Indicating whether control payment term or not.")]
        public bool AllowPaymentTerm
        {
            get
            {
                return this.ib_AllowPaymentTerm;
            }
            set
            {
                this.ib_AllowPaymentTerm = value;
            }
        }

        [Category("User's defined properties"), Description("Allow this user post selected invoices."), DefaultValue(false)]
        public bool AllowPostInvoice
        {
            get
            {
                return this.ib_AllowPostInvoice;
            }
            set
            {
                this.ib_AllowPostInvoice = value;
            }
        }

        [DefaultValue(false), Category("User's defined properties"), Description("Indicating whether control product or not.")]
        public bool AllowProductControl
        {
            get
            {
                return this.ib_AllowProductControl;
            }
            set
            {
                this.ib_AllowProductControl = value;
            }
        }

        [DefaultValue(false), Description("Indicating whether control quantity or not."), Category("User's defined properties")]
        public bool AllowQuantityControl
        {
            get
            {
                return this.ib_AllowQuantityControl;
            }
            set
            {
                this.ib_AllowQuantityControl = value;
            }
        }

        [Description("Indicating whether control special tax or not."), Category("User's defined properties"), DefaultValue(false)]
        public bool AllowSpecialTax
        {
            get
            {
                return this.ib_AllowSpecialTax;
            }
            set
            {
                this.ib_AllowSpecialTax = value;
            }
        }

        [Category("User's defined properties"), Description("Allow this user view current invoice."), DefaultValue(false)]
        public bool AllowViewInvoice
        {
            get
            {
                return this.ib_AllowViewInvoice;
            }
            set
            {
                this.ib_AllowViewInvoice = value;
            }
        }

        [DefaultValue(false), Category("User's defined properties"), Description("Use bilingual in invoices.\nFalse-Vietnamese.\nTrue-Vietnamese and English.")]
        public bool BilingualUsed
        {
            get
            {
                return (AppInstance.Bilingual == 1);
            }
        }

        [Description("Indicating whether control damp percent or not."), Category("User's defined properties"), DefaultValue(false)]
        public bool DampPercentControl
        {
            get
            {
                return AppInstance.DampCtrl;
            }
        }

        [DefaultValue(false), Description("Indicating whether control dead line or not."), Category("User's defined properties")]
        public bool DeadLineControl
        {
            get
            {
                return AppInstance.DeadLineCtrl;
            }
        }

        [Category("User's defined properties"), DefaultValue(""), Description("The default account code.")]
        public string DefaultAccountCode
        {
            get
            {
                return this.is_DefaultAccountCode;
            }
            set
            {
                this.is_DefaultAccountCode = value;
            }
        }

        [Description("The default debit or credit."), DefaultValue("N"), Category("User's defined properties")]
        public string DefaultActivate
        {
            get
            {
                return this.is_DefaultActivate;
            }
            set
            {
                this.is_DefaultActivate = value;
            }
        }

        [Description("The default lock status to add invoice."), Category("User's defined properties"), DefaultValue(false)]
        public bool DefaultLockStatus
        {
            get
            {
                return AppInstance.LockDefault;
            }
        }

        [DefaultValue(0), Description("The default status to add invoice."), Category("User's defined properties")]
        public int DefaultPostStatus
        {
            get
            {
                if ("YPWGH".Contains(this.TransactionType))
                {
                    if (!AppInstance.PostDefault)
                    {
                        return 0;
                    }
                    return 2;
                }
                if (!AppInstance.PostDefault)
                {
                    return 0;
                }
                return 1;
            }
        }

        [DefaultValue(""), Category("User's defined properties"), Description("The default process code to add invoice.")]
        public string DefaultProcessCode
        {
            get
            {
                return AppInstance.ProcessCode;
            }
        }

        [DefaultValue(""), Category("User's defined properties"), Description("Copy current invoice to new invoice.")]
        public string DetailFieldsOverride
        {
            get
            {
                return this.is_DetailFieldsOverride;
            }
            set
            {
                this.is_DetailFieldsOverride = value.ToUpper();
            }
        }

        [Description("The detail primary key field name."), DefaultValue("Stt0"), Category("User's defined properties")]
        public string DetailKeyField
        {
            get
            {
                return this.is_DetailKeyField;
            }
            set
            {
                this.is_DetailKeyField = value;
            }
        }

        [Description("The detail order string."), Category("User's defined properties"), DefaultValue("Stt_Nv")]
        public string DetailOrderString
        {
            get
            {
                return this.is_DetailOrderString;
            }
            set
            {
                if (this.is_DetailOrderString != value)
                {
                    this.is_DetailOrderString = value;
                }
            }
        }

        [Category("User's defined properties"), Description("The current english invoice name to edit or add."), DefaultValue("")]
        public string EnglishInvoiceName
        {
            get
            {
                return this.is_EnglishInvoiceName;
            }
            set
            {
                this.is_EnglishInvoiceName = value;
            }
        }

        [Category("User's defined properties"), Description("The current english invoice title to edit or add."), DefaultValue("")]
        public string EnglishInvoiceTitle
        {
            get
            {
                return this.is_EnglishInvoiceTitle;
            }
            set
            {
                this.is_EnglishInvoiceTitle = value;
            }
        }

        [Category("User's defined properties"), Description("The export tax account."), DefaultValue("")]
        public string ExportTaxAccount
        {
            get
            {
                return AppInstance.Account33331;
            }
        }

        [Category("User's defined properties"), DefaultValue((string) null), Description("The referrence to AppInvoiceLibDataAccess.")]
        public AppInvoiceLibDataAccess AppInvoiceMethod
        {
            get
            {
                if (this.oInvoiceMethod == null)
                {
                    this.oInvoiceMethod = new AppInvoiceLibDataAccess();
                }
                return this.oInvoiceMethod;
            }
        }

        [Description("Indicating whether filter invoice by user created or not."), DefaultValue(false), Category("User's defined properties")]
        public bool FilterByUser
        {
            get
            {
                return AppInstance.FilterByUser;
            }
        }

        [Description("The default first free value to add invoice."), Category("User's defined properties"), DefaultValue("")]
        public string FirstFreeCode
        {
            get
            {
                return AppInstance.DefaultCode1;
            }
        }

        [Description("The number of free fields used in the invoice."), DefaultValue(-1), Category("User's defined properties")]
        public int FreeFields
        {
            get
            {
                return this.ii_FreeFields;
            }
            set
            {
                this.ii_FreeFields = value;
            }
        }

        [Category("User's defined properties"), DefaultValue("K"), Description("The current history type to edit or add.")]
        public string HistoryType
        {
            get
            {
                return this.is_HistoryType;
            }
            set
            {
                this.is_HistoryType = value;
            }
        }

        [DefaultValue(""), Description("The import tax account."), Category("User's defined properties")]
        public string ImportTaxAccount
        {
            get
            {
                return AppInstance.Account33332;
            }
        }

        [Description("The value added tax input account."), DefaultValue(""), Category("User's defined properties")]
        public string InputVATAccount
        {
            get
            {
                return AppInstance.Account1331;
            }
        }

        [Description("The current invoice code to edit or add."), DefaultValue(""), Category("User's defined properties")]
        public string InvoiceCode
        {
            get
            {
                return this.is_InvoiceCode;
            }
            set
            {
                if ((this.is_InvoiceCode != value) && !string.IsNullOrEmpty(value))
                {
                    this.is_InvoiceCode = value;
                    if (AppDataAccess.CheckConnected)
                    {
                        this.SetInvoiceParameter();
                        this.SetUserRights();
                    }
                }
            }
        }

        [Category("User's defined properties"), Description("Whether control history of invoice or not."), DefaultValue(false)]
        public bool InvoiceHistory
        {
            get
            {
                return AppInstance.HistoryCtrl;
            }
        }

        [Description("The current invoice name to edit or add."), DefaultValue(""), Category("User's defined properties")]
        public string InvoiceName
        {
            get
            {
                return this.is_InvoiceName;
            }
            set
            {
                this.is_InvoiceName = value;
            }
        }

        [DefaultValue(""), Category("User's defined properties"), Description("The current invoice serial.")]
        public string InvoiceSerial
        {
            get
            {
                return this.is_InvoiceSerial;
            }
            set
            {
                if (this.is_InvoiceSerial != value)
                {
                    this.is_InvoiceSerial = value;
                }
            }
        }

        [Category("User's defined properties"), Description("The current invoice title to edit or add."), DefaultValue("")]
        public string InvoiceTitle
        {
            get
            {
                return this.is_InvoiceTitle;
            }
            set
            {
                this.is_InvoiceTitle = value;
            }
        }

        [Description("The current invoice type to edit or add."), Category("User's defined properties"), DefaultValue("1")]
        public string InvoiceType
        {
            get
            {
                return this.is_InvoiceType;
            }
            set
            {
                this.is_InvoiceType = value;
            }
        }

        [Category("User's defined properties"), Description("Indicating whether control item quality or not."), DefaultValue(false)]
        public bool ItemQualityControl
        {
            get
            {
                return AppInstance.QualityCtrl;
            }
        }

        [Category("User's defined properties"), Description("Indicating whether control lot number or not."), DefaultValue(0)]
        public int LastDaysInvoice
        {
            get
            {
                return AppInstance.DaysFilter;
            }
        }

        [DefaultValue(false), Description("Indicating whether control lot number or not."), Category("User's defined properties")]
        public bool LotNumberControl
        {
            get
            {
                return AppInstance.LotNumberCtrl;
            }
        }

        [Description("Copy current invoice to new invoice."), Category("User's defined properties"), DefaultValue("")]
        public string MasterFieldsOverride
        {
            get
            {
                return this.is_MasterFieldsOverride;
            }
            set
            {
                this.is_MasterFieldsOverride = value.ToUpper();
            }
        }

        [Description("The master primary key field name."), Category("User's defined properties"), DefaultValue("Stt")]
        public string MasterKeyField
        {
            get
            {
                return this.is_MasterKeyField;
            }
            set
            {
                this.is_MasterKeyField = value;
            }
        }

        [Description("The master order string."), DefaultValue("Ngay_Ct, So_Ct"), Category("User's defined properties")]
        public string MasterOrderString
        {
            get
            {
                return this.is_MasterOrderString;
            }
            set
            {
                if (this.is_MasterOrderString != value)
                {
                    this.is_MasterOrderString = value;
                }
            }
        }

        [Category("User's defined properties"), Description("The value added tax output account."), DefaultValue("")]
        public string OutputVATAccount
        {
            get
            {
                return AppInstance.Account33311;
            }
        }

        [DefaultValue(""), Description("The report filename."), Category("User's defined properties")]
        public string ReportFileName
        {
            get
            {
                return this.is_ReportFileName;
            }
            set
            {
                this.is_ReportFileName = value;
            }
        }

        [Description("The sale discount account."), Category("User's defined properties"), DefaultValue("")]
        public string SaleDiscountAccount
        {
            get
            {
                return AppInstance.Account521;
            }
        }

        [DefaultValue("0"), Category("User's defined properties"), Description("Get or set sale discount type.\n0-Discount minus to payment amount.\n1-Discount minus to sale price.")]
        public string SaleDiscountType
        {
            get
            {
                return AppInstance.DiscountType.ToString();
            }
        }

        [Category("User's defined properties"), Description("The default second free value to add invoice."), DefaultValue("")]
        public string SecondFreeCode
        {
            get
            {
                return AppInstance.DefaultCode2;
            }
        }

        [DefaultValue(false), Category("User's defined properties"), Description("Set lock after printed.")]
        public bool SetLockAfterPrinted
        {
            get
            {
                return AppInstance.LockAfterPrint;
            }
        }

        [DefaultValue(0), Category("User's defined properties"), Description("Show message when debit value is over the acceptable value.")]
        public int ShowDebitWarning
        {
            get
            {
                return AppInstance.DebitWarning;
            }
        }

        [Category("User's defined properties"), Description("Show message when stock quantity is fewer than minimum quantity."), DefaultValue(0)]
        public int ShowItemWarning
        {
            get
            {
                return AppInstance.StockWarning;
            }
        }

        [DefaultValue(""), Description("The special tax account."), Category("User's defined properties")]
        public string SpecialTaxAccount
        {
            get
            {
                return AppInstance.Account3332;
            }
        }

        [Description("The default third free value to add invoice."), DefaultValue(""), Category("User's defined properties")]
        public string ThirdFreeCode
        {
            get
            {
                return AppInstance.DefaultCode3;
            }
        }

        [DefaultValue("K"), Description("The current transaction type to edit or add."), Category("User's defined properties")]
        public string TransactionType
        {
            get
            {
                return this.is_TransactionType;
            }
            set
            {
                this.is_TransactionType = value;
            }
        }

        [Category("User's defined properties"), DefaultValue("K"), Description("Indicating whether price included VAT or not.")]
        public string VATInPrice
        {
            get
            {
                if (!AppInstance.VATInPrice)
                {
                    return "K";
                }
                return "C";
            }
        }
    }
}

